package com.tanhua.manage.listener;

import com.alibaba.fastjson.JSON;
import com.tanhua.commons.templates.HuaWeiUGCTemplate;
import com.tanhua.domain.mongo.Publish;
import com.tanhua.dubbo.api.mongo.PublishApi;
import com.tanhua.manage.domain.Log;
import com.tanhua.manage.service.LogService;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.Reference;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.List;
import java.util.Map;

@Component
@RocketMQMessageListener(topic = "tanhua-publish",consumerGroup = "tanhua-publish-consumer")
@Slf4j
public class PublishMessageListener implements RocketMQListener<String> {

    @Reference
    private PublishApi publishApi;

    @Autowired
    private HuaWeiUGCTemplate huaWeiUGCTemplate;

    @Override
    public void onMessage(String publishId) {
        log.info("====开始审核朋友圈=====>"+publishId);
        //根据id查找朋友圈
        Publish publish = publishApi.findById(publishId);
        if(publish!=null){
            //获取朋友圈的状态
            Integer state = publish.getState();
            //检查内容有没有违规
            if(huaWeiUGCTemplate.textContentCheck(publish.getTextContent())){
                //内容不违规,检查图片
                List<String> medias = publish.getMedias();
                if(medias!=null && medias.size()>0) {
                    if (huaWeiUGCTemplate.imageContentCheck(medias.toArray(new String[]{}))) {
                        state = 1;
                    } else {
                        //图片违规
                        state = 2;
                    }
                }
            }else{
                //内容违规
                state=2;
            }
            //更新状态
            publishApi.update(publishId,state);

        }
    }
}
